package com.ideaaedi.plugin.fastcoding.ui;

import com.alibaba.fastjson.JSON;
import com.ideaaedi.commonds.exception.ExceptionUtil;
import com.ideaaedi.commonds.quick.ClipboardUtil;
import com.ideaaedi.plugin.fastcoding.action.MockJsonAction;
import com.ideaaedi.plugin.fastcoding.constant.FastCodingConstant;
import com.ideaaedi.plugin.fastcoding.util.NotifyUtil;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.project.Project;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * json框
 *
 * @author JustryDeng
 * @since 2022/1/6 22:55:47
 */
public class MockJson_ShowJson extends JFrame {
    public JPanel jPanel;
    public JTextArea jsonArea;
    private JButton copyButton;
    private JButton formatButton;
    private JButton copyAndCloseButton;
    
    public MockJson_ShowJson(Project project, String className) {
        // 设置内容面板
        setContentPane(jPanel);
        // 设置标题
        setTitle(FastCodingConstant.PLUGIN_NAME + FastCodingConstant.CONCAT_SIGN_WITH_WHITESPACE + FastCodingConstant.MOCK_JSON_NAME +
                FastCodingConstant.ADVANCE_SIGN_WITH_WHITESPACE + className);
        // 设置默认高亮的按钮
        getRootPane().setDefaultButton(copyAndCloseButton);
    
    
        // 设置大小 和 位置
        setSize(1000, 700);
        setLocationRelativeTo(null);
        
        
        // 添加监听器
        initListener(project);
    }
    
    // 添加监听器
    private void initListener(Project project) {
        // 格式化
        formatButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String jsonText = jsonArea.getText();
                try {
                    String formatJsonText = JSON.toJSONString(JSON.parseObject(jsonText), true);
                    jsonArea.setText(formatJsonText);
                } catch (Exception ex) {
                    NotifyUtil.notify(project, MockJsonAction.ACTION_NAME, ExceptionUtil.getStackTraceMessage(ex), NotificationType.ERROR);
                }
            }
        });
        // 复制
        copyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String jsonText = jsonArea.getText();
                    ClipboardUtil.setClipboardString(jsonText);
                } catch (Exception ex) {
                    NotifyUtil.notify(project, MockJsonAction.ACTION_NAME, ExceptionUtil.getStackTraceMessage(ex), NotificationType.ERROR);
                }
            }
        });
        
        
        // 复制并关闭
        this.setAlwaysOnTop(true);
        copyAndCloseButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String jsonText = jsonArea.getText();
                    ClipboardUtil.setClipboardString(jsonText);
                    dispose();
                } catch (Exception ex) {
                    NotifyUtil.notify(project, MockJsonAction.ACTION_NAME, ExceptionUtil.getStackTraceMessage(ex), NotificationType.ERROR);
                }
            }
        });
    }
    
    {
        // GUI initializer generated by IntelliJ IDEA GUI Designer
        // >>> IMPORTANT!! <<<
        // DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }
    
    /**
     * Method generated by IntelliJ IDEA GUI Designer >>> IMPORTANT!! <<< DO NOT edit this method OR call it in your
     * code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        jPanel = new JPanel();
        jPanel.setLayout(new GridLayoutManager(2, 3, new Insets(0, 0, 0, 0), -1, -1));
        final JScrollPane scrollPane1 = new JScrollPane();
        jPanel.add(scrollPane1, new GridConstraints(0, 0, 1, 3, GridConstraints.ANCHOR_CENTER,
                GridConstraints.FILL_BOTH,
                GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW,
                GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0,
                false));
        jsonArea = new JTextArea();
        jsonArea.setText("");
        scrollPane1.setViewportView(jsonArea);
        formatButton = new JButton();
        formatButton.setText("格式化");
        jPanel.add(formatButton, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER,
                GridConstraints.FILL_HORIZONTAL,
                GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        copyButton = new JButton();
        copyButton.setText("复制");
        jPanel.add(copyButton, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_CENTER,
                GridConstraints.FILL_HORIZONTAL,
                GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        copyAndCloseButton = new JButton();
        copyAndCloseButton.setText("复制并关闭");
        jPanel.add(copyAndCloseButton, new GridConstraints(1, 2, 1, 1, GridConstraints.ANCHOR_CENTER,
                GridConstraints.FILL_HORIZONTAL,
                GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
    }
    
    /** @noinspection ALL */
    public JComponent $$$getRootComponent$$$() {
        return jPanel;
    }
}
